<template>
	<div :style="'display:inline-block;'+(isRelative?'position:relative;':'')" class="printselect">
		<div class="el-button-group" v-if="printTemplates.length>1">
			<button type="button" v-if="!loading" :class="'el-button el-button--'+buttonType"
				@click="click($event.target, 0) ">
				<span>{{printText}}</span>
			</button>
			<button type="button" v-if="loading" :class="'el-button el-button--'+buttonType+' is-loading'">
				<i class="el-icon-loading"></i>
				<span>加载中</span>
			</button>
			<button :style="isRelative?'':'padding-left:0px;padding-right:0'" type="button"
				@click="showMore($event.target)"
				:class="'el-button el-button--'+buttonType+ ' el-dropdown__caret-button'+(loading?' is-loading':'')"
				aria-haspopup="list" aria-controls="dropdown-menu-7493" tabindex="0">
				<span>
					<i v-if="!show" class="el-dropdown__icon el-icon-arrow-down"></i>
					<i v-else class="el-dropdown__icon el-icon-arrow-up"></i>
				</span>
			</button>
		</div>
		<div class="moreContain"
			style=" transform-origin:center top 0px;width:100%; z-index:2040; position:absolute; top:30px; right:0px;">
			<ul class="el-dropdown-menu el-popper" id="dropdown-menu-7493" v-show="show" style="text-align:center;">
				<li v-for="item in printTemplates" :key="item" @click="click($event.target,item.id)"
					class="el-dropdown-menu__item">
					{{item.name}}
				</li>
				<div x-arrow="" class="popper__arrow" style="left:13.5px;"></div>
			</ul>
		</div>
		<div v-if="printTemplates.length==1">
			<button type="button" v-if="!loading" :class="'el-button el-button--'+buttonType "
				@click="click($event.target,0)">
				<span>{{printText}}</span>
			</button>
			<button type="button" v-if="loading" :class="'el-button el-button--'+buttonType+' is-loading'">
				<i class="el-icon-loading"></i>
				<span>加载中</span>
			</button>
		</div>
	</div>
</template>

<script>
	export default {
		name: "selectprint",
		props: {
			temlateTypeId: {
				type: Number | String,
				default: "GetPrintTemlate"
			},
			printText: {
				type: String,
				default: '打印'
			},
			buttonType: {
				type: String,
				default: 'text'
			},
			detailRow: {
				type: Object | Number | Array,
				default: 0
			},
			isRelative: {
				type: Boolean,
				default: false
			},
			loading: {
				type: Boolean,
				default: false
			},
			callBack: {
				type: Function,
				default: function(id, val) {
					var condition = {
						printTemplateID: id === 0 ? this.defaultPrintTemplateID : id,
						formateString: $.isArray(val) ? val : [val]
					};
					top.Common.GetPrint(condition);
				}
			},
			printTemplate: {
				type: Array,
				default: null
			}
		},
		data: function() {
			return {
				defaultPrintTemplateID: 0,
				printTemplates: [],
				printTemplateID: 0,
				show: false
			}
		},
		methods: {
			GetPrintTemlate: function() {
				if (this.printTemplate != null) {
					if (this.printTemplate.length > 0) {
						this.printTemplates = Object.assign([], this.printTemplate);
						for (var i = 0; i < this.printTemplates.length; i++) {
							var row = this.printTemplate[i];
							if (row.IsDefault) {
								this.defaultPrintTemplateID = row.id;
								break;
							}
						}
						return;
					}
				}
				//  var MainPrintTemlate = window.top.MainPrintTemlate;
				if (window.top.MainPrintTemlate == null) window.top.MainPrintTemlate = {};
				if (window.top.MainPrintTemlate[this.temlateTypeId] != null) {
					this.printTemplates = window.top.MainPrintTemlate[this.temlateTypeId].printTemplates;
					this.defaultPrintTemplateID = window.top.MainPrintTemlate[this.temlateTypeId].printTemplateID;
					this.printTemplateID = window.top.MainPrintTemlate[this.temlateTypeId].printTemplateID;
					return;
				}
				Common.GetPrintTemlateByTypeID = function(TypeID, callback) {
					Common.Ajax({
						url: '/System/GetPrintTemplateByTypeID',
						type: 'POST',
						async: false,
						data: {
							type: TypeID
						},
						success: function(result) {
							if (result.Success) {
								if (typeof callback == 'function') {
									var list = [];
									var defaultID = 0;
									for (var i = 0; i < result.Result.length; i++) {
										var row = result.Result[i];
										list.push({
											id: row.PrintTemplateID,
											name: row.PrintTemplateName,
											IsDefault: row.IsDefault,
											PrintType: row.PrintType
										});
										if (row.IsDefault || i === 0) {
											defaultID = row.PrintTemplateID;
										}
									}
									callback(list, defaultID);
								}
							}
						},
						complete: function() {
							//  self.listLoading = false;
						}
					});
				};
				Common.GetPrintTemlateByTypeID(this.temlateTypeId, function(result, printTemplateID) {
					this.printTemplates = result;
					this.defaultPrintTemplateID = printTemplateID;
					this.printTemplateID = printTemplateID;

					window.top.MainPrintTemlate[this.temlateTypeId] = {};
					window.top.MainPrintTemlate[this.temlateTypeId].printTemplates = result;
					window.top.MainPrintTemlate[this.temlateTypeId].printTemplateID = printTemplateID;
				});
			},
			click: function(target, id) {
				//this.show=!this.show;
				id = id == 0 ? this.defaultPrintTemplateID : id;
				var val = this.detailRow;
				var defaultClick = function(a) {
					var v = a | val;
					var condition = {
						printTemplateID: id === 0 ? this.defaultPrintTemplateID : id,
						formateString: $.isArray(v) ? v : [v]
					};
					Common.GetPrint = function(condition, id) {
						var options = Common.GetPrintOptions(condition, id);
						var optionsMulti = [];
						optionsMulti.push(options);
						Common.Print(optionsMulti);
					};
					Common.GetPrintOptions = function(condition, id) {
						var options = {};
						var url = '/System/Print';
						Common.Ajax({
							url: url,
							type: 'POST',
							data: condition,
							async: false,
							success: function(result) {
								if (result.Success) {
									options = result.Result;
								} else {
									Common.alert(result.Message);
								}
							},
							complete: function() {
								if (app != null && app.loading != null) {
									app.loading = false;
								}
							}
						});
						return options;
					};
					Common.Print = function(optionsMulti) {
						var defaults = {
							Title: '',
							intTop: 0,
							intLeft: 0,
							IntWidth: '21.00cm',
							IntHeight: '29.70cm',
							Content: ''
						};
						LODOP = getLodop(document.getElementById('LODOP_X'), document.getElementById(
							'LODOP_EM'));
						// LODOP.PRINT_INIT(optionsMulti[0].Title);
						var IsExitContent = false;
						for (var i = 0; i < optionsMulti.length; i++) {
							var options = optionsMulti[i];

							options = $.extend(defaults, options || {});
							if (options.Content == '') {
								continue;
							}
							IsExitContent = !IsExitContent ? true : IsExitContent;
							if (i > 0) {
								LODOP.NewPageA();
							}


							//LODOP.SET_PRINT_PAGESIZE(1, 2200, 0, "A4");
							//LODOP.SET_SHOW_MODE("SKIN_CUSTOM_COLOR", ' #CCCCCC');
							//  LODOP.PRINT_INITA(0, 0, options.IntWidth, options.IntHeight, options.Title);
							if (options.IntHeight == '' || options.IntHeight == null) {
								//    LODOP.PRINT_INITA(0, 0, 577, 10000, "鎵撳嵃鎺т欢鍔熻兘婕旂ず_Lodop鍔熻兘_涓嶅悓楂樺害骞呴潰");
								LODOP.SET_PRINT_PAGESIZE(3, options.IntWidth, 45, options.Title);
								options.Content = options.Content.replace('{width}', options.IntWidth);
							} else {
								LODOP.SET_PRINT_PAGESIZE(1, options.IntWidth, options.IntHeight, options
									.Title);

								options.Content = options.Content.replace('{height}', options.IntHeight);
								// options.Content = options.Content.replace('{width}', options.IntWidth);
								//if ($(options.Content).find('body').children[0].height > $(options.Content).find('body').height())
								//{

								//}

							}
							LODOP.SET_PRINT_MODE("RESELECT_PAGESIZE", true);
							var link = '<link rel="stylesheet" href="/Scripts/Lodop6.218/print.css" />';
							options.Content = link + ' ' + options.Content;
							options.Content = '<div style="padding:0px 5px;">' + options.Content + '</div>';

							var contet = $(options.Content);

							options.IntHeight = options.IntHeight == '' ? contet.height() : options.IntHeight;
							LODOP.ADD_PRINT_HTM(options.intTop, options.intLeft, '100%', '98%', options
								.Content);


						}
						if (IsExitContent) {
							//LODOP.SET_PREVIEW_WINDOW(1, 1, 1, 800, 600, "");//鎵撳嵃鍓嶅脊鍑洪€夋嫨鎵撳嵃鏈虹殑瀵硅瘽妗�	
							LODOP.SET_PRINT_MODE("AUTO_CLOSE_PREWINDOW", 1); //鎵撳嵃鍚庤嚜鍔ㄥ叧闂瑙堢獥鍙�
							LODOP.PREVIEW();
							// LODOP.PRINT_DESIGN();
						}
					};
					top.Common.GetPrint(condition);
				}
				this.callBack(id, val, defaultClick);
			},
			showMore: function(target) {
				this.show = !this.show;
				if (this.show && !this.isRelative) {
					var obj = $(target).parents('.printselect')[0];
					var top = obj.offsetTop + obj.clientHeight;
					var left = obj.offsetLeft;
					if (obj.offsetParent.offsetHeight + obj.offsetParent.offsetTop + $($(obj).find('.el-popper')[0])
						.outerHeight(true) > obj.offsetParent.offsetParent.offsetHeight ||
						obj.offsetHeight + obj.getBoundingClientRect().top + $($(obj).find('.el-popper')[0])
						.outerHeight(true) > $(window).height())
						top = obj.offsetTop - $($(obj).find('.el-popper')[0]).outerHeight(true);

					$(obj).find('.moreContain').css({
						top: top + 'px',
						left: left + 'px'
					});
				}
			}
		},
		created: function() {
			if (top.window.LoadCLodopJS == null) {
				var s = top.document.createElement('script');
				s.src = '/static/js/Lodop6.226_Clodop4.127/LodopFuncs.js';
				top.document.body.appendChild(s);
				top.window.LoadCLodopJS = true;
			}
			//this.GetPrintTemlate();
			$(document).bind("click", function(e) {
				if ($(e.target).closest(".el-dropdown__caret-button").length == 0) {
					this.show = false;
				}
			});
		}
	}
</script>
